// 新建用户 
// mixNick: mixNick,
//     nickName: data.nickName,

exports.createOne = async (params, cloud) => {
  try {
    //先判断是否存在当前主用户ids
    let sql = `INSERT INTO winning (gmt_create,gmt_modified,mixNick) VALUES(now(),now(),'${params.mixNick}');`
    let data = await cloudfn(cloud, sql);
    return data;
  } catch (err) {
    return err;
  }
}

/**
 * 查询用户信息
 */
exports.findUserWinning = async (mixNick, cloud) => {
  try {
    let sql = `select * from winning where mixNick = '${mixNick}' and  gmt_create > '2020-11-04 00:00:00'`;
    let data = await cloudfn(cloud, sql);
    return data[0];
  } catch (err) {
    return err;
  }

};



exports.findCountByMixnick = async (mixNick, cloud) => {
  try {
   let sql = `select count(1) as count from winning where mixnick = '${mixNick}'`;
    let data = await cloudfn(cloud, sql);
        console.log("===========data[0].count==============",data[0].count)
    return data[0].count;

  } catch (err) {
    return err;
  }

};

/**
* 添加nick_name
*   mixNick: mixNick,
  name: data.name,
  phone: data.phone,
  address: data.address
*/
exports.updateUserWinning = async (params, cloud) => {
  try {
    let sql = `update winning set name = '${params.name}',nickName='${params.nickName}',phone= '${params.phone}',address='${params.address}' where mixnick = '${params.mixNick}'`;
    console.log("-------------",sql)
    let data = await cloudfn(cloud, sql);
    return data;
  } catch (err) {
    return err;
  }
}

/**
 * 查询中奖名单
 */
exports.findWinning = async (cloud) => {
  try {
    let sql = `select CONCAT_WS(
							"",
							LEFT (mixnick, 1),
							"****"
						 ) AS mixnick ,DATE_FORMAT(gmt_create,'%m月%d日') as createDate from winning where gmt_create > '2020-11-04 00:00:00'  order by gmt_create asc`;
    let data = await cloudfn(cloud, sql);
    return data;
  } catch (err) {
    return err;
  }
};

exports.todayWinning = async (cloud) => {
  try {
   let sql = `select count(1) as count from winning where  DATE_FORMAT(gmt_create,'%m.%d') = DATE_FORMAT(now(),'%m.%d')`;
    let data = await cloudfn(cloud, sql);
        console.log("===========data[0].count==============",data[0].count)
    return data[0].count;
  } catch (err) {
    return err;
  }

};


cloudfn = async (cloud, sql) => {
  console.log("==============sql==================", sql)
  let cbData = await cloud.dataspace.executeSql(sql, [])
  console.log("==============cbData==================", cbData)
  return cbData
}




